package com.example.demo.controller;

import com.example.demo.VO.Result;
import com.example.demo.entity.Choice;
import com.example.demo.entity.Judgment;
import com.example.demo.entity.Short1;
import com.example.demo.service.JudgmentService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (Judgment)表控制层
 *
 * @author hjl
 * @since 2020-11-30 22:43:01
 */
@CrossOrigin
@RestController
@RequestMapping("judgment")
@Api(value = "判断题", tags = {"判断题操作接口"})
public class JudgmentController {

    /**
     * 服务对象
     */
    @Autowired
    private JudgmentService judgmentService;

    @GetMapping("selectOne")
    public Judgment selectOne(Integer id) {
        return this.judgmentService.queryById(id);
    }

    /**
     * 根据属性查询 （当参数为空时候）查询所有
     *
     * @param judgment
     * @return
     */
    @GetMapping("list")
    public ResponseEntity<List<Judgment>> query(@RequestBody(required = false) Judgment judgment) {
        return ResponseEntity.ok(judgmentService.queryByExample(judgment));
    }

    /**
     * 插入一条数据
     *
     * @param judgment
     * @return
     */
    @PostMapping("add")
    public ResponseEntity<List<Judgment>> add(@RequestBody Judgment judgment) {
        if (judgment.getId() != null) {
            judgmentService.update(judgment);
        } else {
            judgmentService.insert(judgment);
        }
        return ResponseEntity.ok(judgmentService.queryByExample(new Judgment()));
    }

    /**
     * 根据主键更新数据
     *
     * @param judgment
     * @return
     */
    @PutMapping("update")
    public ResponseEntity<List<Judgment>> update(@RequestBody Judgment judgment) {
        judgmentService.update(judgment);
        return ResponseEntity.ok(judgmentService.queryByExample(new Judgment()));
    }

    /**
     * 根据主键删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("del/{id}")
    public ResponseEntity<List<Judgment>> delete(@PathVariable("id") Integer id) {
        judgmentService.deleteById(id);
        return ResponseEntity.ok(judgmentService.queryByExample(new Judgment()));
    }

    @PostMapping("delAll")
    public ResponseEntity<List<Judgment>> deleteAll(@RequestBody Integer[] id) {
        for (int i = 0; i < id.length; i++) {
            judgmentService.deleteById(id[i]);
        }
        return ResponseEntity.ok(judgmentService.queryByExample(new Judgment()));
    }

    @PostMapping("findJudgmentByCourseid")
    public Result findJudgmentByCourseid(Integer courseId) {
        Judgment judgment = new Judgment();
        judgment.setObj2(courseId + "");
        return Result.success(judgmentService.queryByExample(judgment));
    }
}